package com.duanjw.springcloud.account.web;

import com.alibaba.fastjson.JSONObject;
import com.duanjw.springcloud.account.feign.OrderService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 账户controller
 *
 * @author duanjw
 */
@RestController
public class AccountController {

    private RestTemplate restTemplate;
    private OrderService orderService;

    public AccountController(RestTemplate restTemplate, OrderService orderService) {
        this.restTemplate = restTemplate;
        this.orderService = orderService;
    }

    @GetMapping
    public String account() {
        return "account";
    }

    /**
     * 使用restTemplate调用订单接口
     *
     * @return
     * @see com.duanjw.springcloud.account.config.RestTemplateConfig restTemplate配置Ribbon负载均衡
     * 调用地址：curl localhost:7002/order-rest-template
     */
    @GetMapping("order-rest-template")
    public String orderByRestTemplate() {
        // restTemplate要使用Ribbon负载均衡，那么地址应该写`http://[服务名]/[接口地址]`
        String orderResult = restTemplate.getForObject("http://order", String.class);
        return orderResult;
    }


    /**
     * 使用 feign 调用订单接口
     * 调用地址：curl localhost:7002/order-feign
     *
     * @return
     */
    @GetMapping("order-feign")
    public String orderByFeign() {
        return orderService.order();
//        return orderService.order();
    }

    @PostMapping
    public String post(@RequestBody Map map) {
        System.out.println(map);
        map.put("key", "value");
        return map.toString();
    }


    /**
     * 接口限流
     *
     * @return
     * @see com.duanjw.springcloud.account.config.RestTemplateConfig restTemplate配置Ribbon负载均衡
     * 调用地址：curl localhost:7002/order-limiting
     */
    @GetMapping("order-limiting")
    @HystrixCommand(fallbackMethod = "fallbackMethod", threadPoolKey = "order", threadPoolProperties = {@HystrixProperty(name = "coreSize", value = "10")})
    public String orderByLimiting() throws Exception {
        System.out.println("开始等待500毫秒");
        Thread.sleep(500);
        return "order";
    }


    @GetMapping("temp")
    public String temp() {


        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

        //设置接收返回值的格式为json
        List<MediaType> mediaTypeList = new ArrayList<>();
        mediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);
        headers.setAccept(mediaTypeList);

        HttpEntity<JSONObject> entity = new HttpEntity<JSONObject>(new JSONObject(), headers);

        ResponseEntity<String> responseEntity;


        responseEntity = restTemplate.exchange("http://localhost:7002", HttpMethod.POST, entity, String.class);
        String body = responseEntity.getBody();
//        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
//        objectObjectHashMap.put("a","value");
//        JSONObject s = restTemplate.postForObject("http://localhost:7002",new JSONObject() , JSONObject.class);
        return body;
    }
}
